package com.app.aipass.viewmodel;

import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.databinding.ObservableField;

import com.app.aipass.R;
import com.app.aipass.app.AppApplication;
import com.app.aipass.entity.TransactionPlanEntity;
import com.app.aipass.fragment.main.TransactionFragment;

import me.goldze.mvvmhabit.base.ItemViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;

/**
 * @author: woking
 * @date: 2021/6/7
 * @description：
 */
public class TransactionTabItemViewModel extends ItemViewModel<TransactionTabViewModel> {
    public ObservableField<TransactionPlanEntity> entity = new ObservableField<>();
    public Drawable drawableImg;

    public TransactionTabItemViewModel(@NonNull TransactionTabViewModel viewModel, TransactionPlanEntity entity) {
        super(viewModel);
        this.entity.set(entity);
        //ImageView的占位图片，可以解决RecyclerView中图片错误问题
        drawableImg = ContextCompat.getDrawable(viewModel.getApplication(), R.mipmap.ic_launcher);
    }

    public String setPrice()
    {
        return AppApplication.pricetag + entity.get().price;//return "￥" + entity.get().price;
    }

    public String setType()
    {
        if(entity.get().type == 1)
        {
            return "购买";
        }
        return "转赠";
    }

    public String getOpenTime()
    {
        return TransactionFragment.openTime;
    }
    public int setOpenTime()
    {
        if(getPosition() == 0)
        {
            return View.VISIBLE;
        }
        return View.GONE;
    }
    public int isBankImg()
    {
        if(entity.get().pay_types.contains("1"))
        {
            return View.VISIBLE;
        }
        return View.GONE;
    }
    public int isZFBImg()
    {
        if(entity.get().pay_types.contains("2"))
        {
            return View.VISIBLE;
        }
        return View.GONE;
    }
    public int isWXImg()
    {
        if(entity.get().pay_types.contains("3"))
        {
            return View.VISIBLE;
        }
        return View.GONE;
    }
//    public Drawable setImageType()
//    {
//        TransactionPlanEntity mPayAccountEntity = entity.get().pay_types;
//        if(mPayAccountEntity.type == 1)
//        {
//            return ContextCompat.getDrawable(viewModel.getApplication(), R.mipmap.paytype_bank);
//        }
//        else if(mPayAccountEntity.type == 2)
//        {
//            return ContextCompat.getDrawable(viewModel.getApplication(), R.mipmap.paytype_zfb);
//        }
//        else if(mPayAccountEntity.type == 3)
//        {
//            return ContextCompat.getDrawable(viewModel.getApplication(), R.mipmap.paytype_wx);
//        }
//        return ContextCompat.getDrawable(viewModel.getApplication(), R.mipmap.paytype_bank);
//    }
    /**
     * 获取position的方式有很多种,indexOf是其中一种，常见的还有在Adapter中、ItemBinding.of回调里
     *
     * @return
     */
    public int getPosition() {
        return viewModel.getItemPosition(this);
    }

    //条目的点击事件
    public BindingCommand itemClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {

            Log.e("TAG", getPosition()+"=============条目的点击事件=================");

            //这里可以通过一个标识,做出判断，已达到跳入不同界面的逻辑
//            if (entity.get().getId() == -1) {
//                viewModel.deleteItemLiveData.setValue(TransactionTabItemViewModel.this);
//            } else {
//                //跳转到详情界面,传入条目的实体对象
//                Bundle mBundle = new Bundle();
//                mBundle.putParcelable("entity", entity.get());
//                viewModel.startContainerActivity(DetailFragment.class.getCanonicalName(), mBundle);
//            }
        }
    });
    //条目的长按事件
    public BindingCommand itemLongClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            Log.e("TAG", getPosition()+"=============条目的长按事件=================");
            //以前是使用Messenger发送事件，在NetWorkViewModel中完成删除逻辑
//            Messenger.getDefault().send(NetWorkItemViewModel.this, NetWorkViewModel.TOKEN_NETWORKVIEWMODEL_DELTE_ITEM);
            //现在ItemViewModel中存在ViewModel引用，可以直接拿到LiveData去做删除
//            ToastUtils.showShort(entity.get().getName());
        }
    });

    //条目的长按事件
    public BindingCommand onClickBuySell = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
//            Log.e("TAG", getPosition()+"=============onClickBuySell=================");
            //以前是使用Messenger发送事件，在NetWorkViewModel中完成删除逻辑
//            Messenger.getDefault().send(NetWorkItemViewModel.this, NetWorkViewModel.TOKEN_NETWORKVIEWMODEL_DELTE_ITEM);
            //现在ItemViewModel中存在ViewModel引用，可以直接拿到LiveData去做删除
//            ToastUtils.showShort(entity.get().getName());

            viewModel.buySellItemLiveData.setValue(TransactionTabItemViewModel.this);
        }
    });


//    /**
//     * 可以在xml中使用binding:currentView="@{viewModel.titleTextView}" 拿到这个控件的引用, 但是强烈不推荐这样做，避免内存泄漏
//     **/
//    private TextView tv;
//    //将标题TextView控件回调到ViewModel中
//    public BindingCommand<TextView> titleTextView = new BindingCommand(new BindingConsumer<TextView>() {
//        @Override
//        public void call(TextView tv) {
//            NetWorkItemViewModel.this.tv = tv;
//        }
//    });
}